package cn.itsource.common;

import cn.itsource.constants.RConst;
import cn.itsource.exception.GlobalException;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.UUID;

@Component
@Data
@Slf4j
public class TxCos {
    @Value("${txcos.secretId}")
    private String secretId;
    @Value("${txcos.secretKey}")
    private String secretKey;
    @Value("${txcos.regionName}")
    private String regionName;
    @Value("${txcos.bucketName}")
    private String bucketName;
    @Value("${txcos.basePath}")
    private String basePath;

    private String resultUrl = "https://%s.cos.%s.myqcloud.com/%s";
    public String uploadImg(File uploadFile,String suffix){
        if(!suffix.startsWith(RConst.Str.DOT)){
            throw new GlobalException("后缀名错误");
        }
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        Region region = new Region(regionName);
        ClientConfig clientConfig = new ClientConfig(region);
        clientConfig.setHttpProtocol(HttpProtocol.https);
        COSClient cosClient = new COSClient(cred, clientConfig);
        String fileName = UUID.randomUUID().toString();
        //driver + "/" + uuID + .jpg .png .xx
        String key = basePath + RConst.Str.SLASH + fileName + suffix;
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, uploadFile);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        log.info(putObjectResult.getDateStr());
        cosClient.shutdown();
        String url = String.format(resultUrl, bucketName, regionName, key);
        return url;
    }

    public static void main(String[] args) {

        String secretId = "AKIDumhm4OFg9WBtghq5xpkeTPPM7tham2Qx";//用户的 SecretId，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
        String secretKey = "GnSIJRg4fI25DRl9kFgAlLuaKAHYzY7u";//用户的 SecretKey，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的地域, COS 地域的简称请参见 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region("ap-chongqing");
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        // 从 5.6.54 版本开始，默认使用了 https
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);

        // 指定要上传的文件
        File localFile = new File("C:\\Users\\Administrator\\Pictures\\Screenshots\\wojj.jpg");
        // 指定文件将要存放的存储桶
        String bucketName = "fccar-0228-lgh-1327916541";
        // 指定文件上传到 COS 上的路径，即对象键。例如对象键为 folder/picture.jpg，则表示将文件 picture.jpg 上传到 folder 路径下
        String key = "driver/test2.jpg";
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);

        System.out.println(putObjectResult.getDateStr());
        cosClient.shutdown();
    }
}
